﻿$(document).ready(function () {
    var J_game = $("#J_game");
    var url = $("#J_gameget").val();
    var J_game_selected = $("#J_game_selected");
    var J_turnleft = $("#J_turnleft");
    var J_turnright = $("#J_turnright");
    var J_form = $("#J_form");
    var J_form_submit = J_form.find("button[type='submit']");
    var J_form_status = $("#J_formstatus");
    J_game.find("li").live("click", function () {
        var t = $(this);
        t.parent().find("li").removeClass("now");
        t.addClass("now");
        //显示已选
        var selectedhtml = "";
        var parent = t.parent().parent().parent();
        var dl = parent.prevAll("dl").andSelf();
        J_game.css({ width: 150 * (dl.length + 1) });
        parent.nextAll("dl").remove();
        var parm = { "random": Math.random() };
        var length = dl.length;
        for (var i = 0; i < length; i++) {
            name = dl.eq(i).find("dt").attr("data-name");
            li = dl.eq(i).find("li[class='now']");
            value = li.attr("data-value");
            label = li.html();
            selectedhtml += "<span><label>" + label + "</label><input type='hidden' name='" + name + "' value='" + value + "' />";
            selectedhtml += (i == (length - 1)) ? "</span>" : "&gt;</span>";
            parm[name] = value;
        };
        J_game_selected.html(selectedhtml);
        //加载数据
        $.getJSON(url, parm, function (data) {
            var dname = data[0];
            var dtitle = data[1];
            var list = data[2];
            var length = dl.length;
            //如果没数据
            if (list.length == 0) {
                J_form_status.val("1");
                J_form_submit.attr("class", "form-btn-next");
                return;
            };
            //有下级的话就隐藏
            parent.find(".game-select-search").css("visibility", "hidden");
            //添加最后一个选中的>符号
            var J_game_selected_span = J_game_selected.find("span");
            J_game_selected_span.eq(J_game_selected_span.length - 1).append("&gt;");
            //如果有数据
            if (length >= 6) {
                J_game.animate({ left: -150 * (length - 5) }, 150);
                J_turnleft.attr("class", "turnleft-enable");
            } else {
                J_turnright.attr("class", "turnright");
            };
            J_form_status.val("0");
            J_form_submit.attr("class", "form-btn-next-disable");
            var listhtml = "<dl><dt data-name='" + dname + "'></dt><dd class='game-select-label'>" + dtitle + "</dd><dd class='game-select-content'><div class='game-select-search'><input type='text'></div><ul>";
            for (var i = 0; i < list.length; i++) {
                listhtml += "<li data-value='" + list[i].id + "' data-list='" + list[i].list + "'>" + list[i].name + "</li>";
            };
            listhtml += "</ul></dd></dl>";
            parent.after(listhtml);
            //如果只有一个，则点击
            if (list.length == 1) {
                parent.next("dl").find("li").click();
            };
            //如果是要选中项目 加载默认选中项
            if (dname != "gameId") {
                return;
            }
            var selecturl = J_game.attr("data-load");
            $.getJSON(selecturl, parm, function (data) {
                var name = "" + data["gameId"];
                //如果没数据
                if (name.length < 1) { return; }
                //如果有数据
                var selectbd = parent.next("dl").find("li");
                var length = selectbd.length;
                for (var i = 0; i < length; i++) {
                    if (name == selectbd.eq(i).attr("data-value")) {
                        selectbd.eq(i).click();
                        return;
                    }
                };
            });
        });
    });
    //搜索事件
    J_game.find(".game-select-search input").live("keyup", function () {
        J_form_status.val("0");
        J_form_submit.attr("class", "form-btn-next-disable");
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "").toLowerCase();
        var li = t.parent().next("ul").find("li");
        var length = li.length;
        if (val.length < 1) {
            li.show();
            return;
        };
        for (var i = 0; i < length; i++) {
            var temp = ("," + li.eq(i).attr("data-list") + ",").toLowerCase();
            var vals = li.eq(i).html().toLowerCase();
            if (temp.indexOf(val) > -1 || vals.indexOf(val) > -1) {
                li.eq(i).show();
            } else {
                li.eq(i).hide();
            };
        };
    });
    //初始化数据
    //加载数据
    var parm = { "random": Math.random() };
    $.getJSON(url, parm, function (data) {
        var dname = data[0];
        var dtitle = data[1];
        var list = data[2];
        //如果没数据
        if (list.length == 0) { J_form_status.val("1"); J_form_submit.attr("class", "form-btn-next"); return; }
        //如果有数据
        J_form_status.val("0");
        J_form_submit.attr("class", "form-btn-next-disable");
        var listhtml = "<dl><dt data-name='" + dname + "'></dt><dd class='game-select-label'>" + dtitle + "</dd><dd class='game-select-content'><div class='game-select-search'><input type='text'></div><ul>";
        for (var i = 0; i < list.length; i++) {
            listhtml += "<li data-value='" + list[i].id + "' data-list='" + list[i].list + "'>" + list[i].name + "</li>";
        };
        listhtml += "</ul></dd></dl>";
        J_game.html(listhtml);
        //如果只有一个，则点击
        if (list.length == 1) {
            J_game.find("li").click();
        };
    });

    //左右切换
    J_turnleft.click(function () {
        var lefts = parseInt(J_game.css("left"));
        if (isNaN(lefts)) { lefts = 0; }
        lefts += 150;
        if (lefts > 0) { return; };
        J_game.animate({ left: lefts }, 150);
        if (lefts >= 0) { J_turnleft.attr("class", "turnleft"); return; };
        J_turnleft.attr("class", "turnleft-enable");
        J_turnright.attr("class", "turnright-enable");
    });
    J_turnright.click(function () {
        var lefts = parseInt(J_game.css("left"));
        if (isNaN(lefts)) { lefts = 0; }
        var length = J_game.find("dl").length;
        if (lefts < -(length - 7) * 150) { return; }
        lefts += -150;
        J_game.animate({ left: lefts }, 150);
        if (lefts <= -(length - 6) * 150) { J_turnright.attr("class", "turnright"); return; }
        J_turnleft.attr("class", "turnleft-enable");
    });
    //表单监测
    J_form.submit(function () {
        if (J_form_status.val() == "0") { return false; }
        return true;
    });
});
//在线客服
var snda_os_webrefer_ufg = {
    gm: 120802,
    triggers: {
        floatwin: ["010101", "010102"],
        extend: [{ triggerid: "J_olservice", source: "010103"}]
    },
    t: "盛大商城-卖家相关问题",
    hidewidgetwindow: true
};
$("#J_olservice").attr("href", "javascript:void(0)");